$(document).on('click', '#qa_submit', function(){ const name = ($('#qa_name').val() || '').trim(); const unit = ($('#qa_unit').val() || '').trim(); const pieces = parseInt($('#qa_pieces').val() || 1, 10); const purch = parseFloat($('#qa_purchase').val() || 0); const sale = parseFloat($('#qa_sale').val() || 0); if (!name) { Swal.fire('Missing', 'Item Name is required', 'warning'); return; } $.ajax({ url: "{% url 'sales:ajax_quick_create_product' %}", type: "POST", headers: { 'X-CSRFToken': csrftoken }, data: { name: name, unit: unit, pieces_per_pack: pieces, purchase_price: purch, l_sale_price: sale }, success: function(res){ if (!res || !res.ok) { Swal.fire('Failed', (res && res.error) ? res.error : 'Unknown error', 'error'); return; } // Close modal $('#productQuickAddModal').modal('hide'); // Close any open select2 dropdown (just in case) try { $('.search-product').select2('close'); } catch(_){} // Pick active row (the row whose select2 initiated this) const $row = (window.__activeProductRow && window.__activeProductRow.length) ? window.__activeProductRow : $('#table_products tbody tr').not('.template-row').last(); // Feed the new product into this row and jump to Qty fillRow($row, res); recalcAll(); // Auto-select & jump to Qty (as you wanted) const $qty = $row.find('.qty-input'); if ($qty.length) { $qty.focus(); try { $qty[0].select(); } catch(_){} } }, error: function(xhr){ const msg = (xhr.responseJSON && xhr.responseJSON.error) || xhr.statusText || 'Server error'; Swal.fire('Create failed', msg, 'error'); } }); }); $('#productQuickAddModal').on('shown.bs.modal', function(){ setTimeout(()=> $('#qa_name').trigger('focus').select(), 150); }); $('#productQuickAddModal').on('hidden.bs.modal', function(){ $('body').removeClass('qa-modal-open'); }); const order = ['qa_name','qa_unit','qa_pieces','qa_purchase','qa_sale']; $(document).on('keydown', '#productQuickAddModal input, #productQuickAddModal select', function(e){ if (e.key === 'Enter') { e.preventDefault(); const idx = order.indexOf(this.id); if (idx > -1 && idx < order.length - 1) { const nextId = order[idx + 1]; const $n = $('#'+nextId); $n.focus(); if ($n.is('input')) { try { $n[0].select(); } catch(_){} } } else if (idx === order.length - 1) { // last field -> submit $('#qa_submit').click(); } } });